package solution.design;

import java.util.Random;

/**
 * @author zhangmin
 * @create 2021-08-11 12:18
 * 设计问题--打乱数组
 */
public class Solution {
    int[] nums;
    private Random rand;


    public Solution(int[] nums) {
        this.nums=nums;
        rand=new Random();
    }

    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        return this.nums;
    }

    void swap(int[] nums,int i,int j){
        if (i!=j) {
            int t = nums[i];
            nums[i] = nums[j];
            nums[j] = t;
        }
    }
    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        if (nums==null)
            return null;
        int[] shufflenums=nums.clone();
        for (int i = 1; i < shufflenums.length; i++) {
            int index= rand.nextInt(i+1);
            swap(shufflenums,i,index);
        }
        return shufflenums;
    }

    public static void main(String[] args) {
        Solution solution=new Solution(new int[]{1, 2, 3});
        int[] res1=solution.shuffle();
        int[] res2=solution.reset();
        int[] res3=solution.shuffle();
        System.out.println(res1.toString());
        for (int res:  res1) {
            System.out.println(res);
        }
        System.out.println(res2.toString());
        for (int res:  res2) {
            System.out.println(res);
        }
        System.out.println(res3.toString());
        for (int res:  res3) {
            System.out.println(res);
        }
    }
}
